package com.itheima.service.impl;

import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

@Service
public class OrderSettingImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 预约管理列表
     * @param month
     * @return
     */
    public List<Map<String,String>> getOrderSettingByMonth(String month) {
        String temp = month.substring(month.indexOf("-")+1, month.length());
        if(temp.length() == 1){
            month = month.substring(0, month.indexOf("-") +1) + "0" + temp;
        }
        LocalDate begin = LocalDate.parse(month + "-01");
        LocalDate end = LocalDate.parse(month + "-" + begin.lengthOfMonth());

        List<OrderSetting> orderSettingList = orderSettingDao.findByMonth(begin, end);
        List<Map<String,String>> ls = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            Map map = new HashMap();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(orderSetting.getOrderDate());
            int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); // 获取当前月的第几天
            map.put("date", dayOfMonth);
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            ls.add(map);
        }
        return ls;
    }

    /**
     * 批量添加预约表数据
     * @param ls
     */
    public void insertBatch(List<Map<String, String>> ls) {
        orderSettingDao.insertBatch(ls);
    }

    /**
     * 修改可预约人数
     * @param orderDate
     */
    public void updateNumber(Integer number, String orderDate) {
        OrderSetting orderSetting = orderSettingDao.getByOrderDate(orderDate);
        if(orderSetting == null){
            orderSettingDao.insert(orderDate, number, 0);
        }else {
            orderSettingDao.updateNumber(number, orderDate);
        }
    }
}
